<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\model\StudyStudentList as M;
use app\common\validate\StudyStudentList as V;
use think\Model;
use think\model\concern\SoftDelete;

class RecruitCustomer extends Model
{
    use SoftDelete;

    protected $deleteTime = false;

    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        //按手机号查找
//        if ($mobile = input("mobile")) {
//            $where[] = ["mobile", "like", "%" . $mobile . "%"];
//        }
        //按姓名查找
        if ($username = input("username")) {
            $where[] = ["username", "like", "%" . $username . "%"];
        }
        //按客户意向查找
        if ($intention = input("intention")) {
            $where[] = ["intention", "like", "%" . $intention . "%"];
        }
        //按邀约状态查找
        if ($customer_type = input("customer_type")) {
            $where[] = ["customer_type", "=",  $customer_type];
        }

        //按客户来源查找
        if ($source_id = input("source_id")) {
            $where[] = ["source_id", "=",  $source_id];
        }


        $list = self::alias('a')
            ->field('a.*,b.invite_type')
            ->join('bl_recruit_customer_phone b','a.id=b.customer_id','left')
            ->with('getSource')
            ->order('id', 'desc')
            ->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public function getSource()
    {
        return $this->hasOne(SchoolCustomerSource::class, 'id', 'source_id');
    }

    public function getAdmin()
    {
        return $this->hasOne(AdminAdmin::class, 'id', 'admin_id');
    }

    public static function getFinanceList()
    {
        $where = [];
        $limit = input('get.limit');

        //按手机号查找
        if ($mobile = input("mobile")) {
            $where[] = ["mobile", "like", "%" . $mobile . "%"];
        }
        //按姓名查找
        if ($username = input("username")) {
            $where[] = ["username", "like", "%" . $username . "%"];
        }

        if ($finance_audit = input('get.finance_audit', '')) {
            $where[] = ["finance_audit", "=", $finance_audit];
        }
        $where[] = ["deal_price", ">", 0];
        $list = self::with([
            'get_source' => function ($a) {
                $a->field('id,source_info');
            }
        ])->order('id', 'desc')->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function getTeacherList()
    {
        $where = [];
        $limit = input('get.limit');
        //按手机号查找
        if ($mobile = input("mobile")) {
            $where[] = ["mobile", "like", "%" . $mobile . "%"];
        }
        //按姓名查找
        if ($username = input("username")) {
            $where[] = ["username", "like", "%" . $username . "%"];
        }
        if ($finance_audit = input('get.finance_audit', '')) {
            $where[] = ["finance_audit", "=", $finance_audit];
        }
        $where[] = ["finance_audit", "=", 2];
        $admin = session('admin');
        $admin_id = AdminAdmin::where('school_id', $admin['school_id'])->column('id');
        $where[] = ["admin_id", "IN", $admin_id];
        $list = self::with([
            'get_source' => function ($a) {
                $a->field('id,source_info');
            }
        ])->order('id', 'desc')->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function getChildList()
    {
        $where = [];
        $limit = input('get.limit');
        $id = input('get.id');
        $where[] = ['customer_id', '=', $id];
        $list = RecruitCustomerBody::order('id','desc')->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    public static function addChildStudent($data,$id)
    {
        //验证
        $validate = new V;
        if (!$validate->scene('add')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        try {
            $admin = session('admin');
            $data['add_time'] = toDate(time());
            $data['school_id'] = $admin['school_id'];
            $mobile = $data['mobile'];
            $data['end_time'] = strtotime($data['end_time'] . ' 23:59:59');
            $parent = \app\common\model\UsersIndex::where('mobile', $mobile)->find();
            if ($parent) {
                $data['parent_id'] = $parent['id'];
            } else {
                return ['msg' => '未查询到家长信息', 'code' => 201];
            }
            $student_id = M::insertGetId($data);
            RecruitCustomerBody::where('id', $id)->update([
                'is_in' => 2,
                'student_id' => $student_id
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }


}
